package whf.framework.security.service;

import java.util.List;
import java.util.Set;

import whf.framework.exception.DAOException;
import whf.framework.exception.FindException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.security.dao.GroupDAO;
import whf.framework.security.dao.RoleDAO;
import whf.framework.security.entity.Group;
import whf.framework.security.entity.Role;
import whf.framework.security.entity.User;
import whf.framework.util.BeanFactory;
import whf.framework.util.BeanInitialization;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class RoleServiceImp extends whf.framework.service.ServiceImp<Role> implements RoleService {
	private static Log log = LogFactory.getLog(RoleServiceImp.class);
	
	private RoleDAO roleDAO;

	private GroupDAO groupDAO;
	/**
	 * @modify wanghaifeng Aug 31, 2006 11:08:57 PM
	 * @param roleDAO
	 */
	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	/**
	 * @modify wanghaifeng Aug 31, 2006 11:11:28 PM
	 * @param groupDAO
	 */
	public void setGroupDAO(GroupDAO groupDAO){
		this.groupDAO = groupDAO;
	}
	
	public static RoleService getRoleService() throws ServiceNotFoundException {
		return (RoleService)BeanFactory.getService(RoleServiceImp.class) ;
	}

	public void assignRoleGroups(Role role, List<Group> groups) throws DAOException {
		List<Group> oldGroups = this.groupDAO.findByRole(role);
		for(Group oldGroup: oldGroups){
			boolean found = false;
			for(Group newGroup: groups){
				if(oldGroup.idEquals(newGroup)){
					found = true;
					break;
				}
			}
			if(!found){
				oldGroup.removeRole(role);
				this.groupDAO.update(oldGroup);
			}
		}
		for(Group group: groups){
			if(!group.contains(role)){
				group.addRole(role);
				this.groupDAO.update(group);
			}
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.service.RoleService#findAnonymousRoles()
	 */
	public Set<Role> findAnonymousRoles() throws FindException{
		Set<Role> roles = Utils.newHashSet();
		BeanInitialization.setInitializeProperties(new String[]{"permissions"});
		List<Role> set = this.roleDAO.findWithoutLazy("t.name = 'anonymous'");
		for(Role bo: set){
			roles.add((Role)bo);
		}
		return roles;
	}

	public Set<Role> findByUser(User user) throws FindException {
		Set<Role> roles = Utils.newHashSet();
		try{
			List<Role> list = this.roleDAO.findByUser(user);
			for(Role r: list){
				BeanInitialization.setInitializeProperties(new String[]{"permissions"});
				Role role = (Role)this.roleDAO.findByPrimaryKeyWithoutLazy(new Long(r.getId()));
				roles.add(role);
			}
		}catch(Exception e){
			log.error(this, e);
			throw new FindException(e);
		}
		return roles;
	}
	
	public Set<User> findRoleUsers(Role role) throws FindException {
		BeanInitialization.setInitializeProperties(new String[]{"users"});
		try{
			Role r = (Role)this.roleDAO.findByPrimaryKeyWithoutLazy(new Long(role.getId()));
			return r.getUsers();
		} catch(Exception e){
			return Utils.newHashSet();
		}
	}
} 